package com.opengw.exception;

import java.util.HashMap;
import java.util.Iterator;
import java.util.ResourceBundle;
import java.util.Set;

import com.opengw.http.HTTPResponse;

public class OpenGWException extends Exception{

	private static final long serialVersionUID = 3893577148107279771L;
	
	public static final String DEFAULT_ERROR_PAGE = "/WEB-INF/exception/defaultErrorPage.jsp";
	
	public static final String UNKNOWN_EXCEPTION = "UNKNOWN_EXCEPTION";
	public static final String IO_EXCEPTION = "IO_EXCEPTION";
	public static final String NOT_FOUND_RESOURCE = "NOT_FOUND_RESOURCE";
	public static final String ROUTE_CONFIG_NOT_FOUND = "ROUTE_CONFIG_NOT_FOUND";
	public static final String CAN_NOT_GET_SSL_CERTIFICATE = "CAN_NOT_GET_SSL_CERTIFICATE";
	public static final String UNSUPPORTED_PROTOCOL = "UNSUPPORTED_PROTOCOL";
	public static final String FORBIDDEN_HTTP_METHOD = "FORBIDDEN_HTTP_METHOD";
	public static final String AUTHENTICATION_FAILED = "AUTHENTICATION_FAILED";
	
	private boolean exceptionMessageHTMLExists;
	private static HashMap<String, String> exceptionPageMap;
	private boolean isHTTPResponseAvaiable;
	private HTTPResponse response;
	
	
	static{
		ResourceBundle bundle = ResourceBundle.getBundle("resources.exceptionPages");
		Set<String> keySet = bundle.keySet();
		Iterator<String> iterator = keySet.iterator();
		exceptionPageMap = new HashMap<String, String>();
		while(iterator.hasNext()){
			String key = iterator.next();
			exceptionPageMap.put(key, bundle.getString(key));
		}
		
	}
	
	public OpenGWException(String message){
		super(message);
		this.exceptionMessageHTMLExists = false;
	}
	
	public OpenGWException(Exception ex){
		super(ex.getCause());
	}
	
	public OpenGWException(String message, boolean exceptionContentHTMLExists){
		super(message);
		this.exceptionMessageHTMLExists = exceptionContentHTMLExists;
	}
	
	public OpenGWException(String message, HTTPResponse response){
		super(message);
		this.isHTTPResponseAvaiable = true;
		this.response = response;
	}

	public void setExceptionMessageHTMLExists(boolean exceptionMessageHTMLExists) {
		this.exceptionMessageHTMLExists = exceptionMessageHTMLExists;
	}

	public boolean isExceptionMessageHTMLExists() {
		return exceptionMessageHTMLExists;
	}

	public String getExceptionPage(){
		if(exceptionMessageHTMLExists)
			return "/WEB-INF/" + exceptionPageMap.get(getMessage());
		else
			return DEFAULT_ERROR_PAGE;
	}

	public void setHTTPResponseAvaiable(boolean isHTTPResponseAvaiable) {
		this.isHTTPResponseAvaiable = isHTTPResponseAvaiable;
	}

	public boolean isHTTPResponseAvaiable() {
		return isHTTPResponseAvaiable;
	}

	public void setResponse(HTTPResponse response) {
		this.response = response;
	}

	public HTTPResponse getResponse() {
		return response;
	}

	
	
	
	

}
